﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuTouTiaoAssist.Util
{
    /// <summary>
    /// 其他工具
    /// </summary>
    class OtherUtil
    {
        /// <summary>
        /// 判断字符串是否为null或者空白字符
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool IsNullOrWhitespace(string str)
        {
            return str == null || str.Trim().Length < 1;
        } 
        
        /// <summary>
        /// 将本地时间转化为unix时间戳，单位为秒
        /// </summary>
        /// <param name="dateTime">本地时间</param>
        /// <returns></returns>
        public static long DateTime2UnixTimeInSeconds(DateTime dateTime)
        {
            return (long)((dateTime.ToUniversalTime() - new DateTime(1970, 1, 1)).Ticks / Math.Pow(10, 7));
        }

        /// <summary>
        /// 将本地时间转化为unix时间戳，单位为毫秒
        /// </summary>
        /// <param name="dateTime">本地时间</param>
        /// <returns></returns>
        public static long DateTime2UnixTimeInMillis(DateTime dateTime)
        {
            return (long)((dateTime.ToUniversalTime() - new DateTime(1970, 1, 1)).Ticks / Math.Pow(10, 4));
        }

        /// <summary>
        /// Unix时间戳（单位：秒），转本地时间
        /// </summary>
        /// <param name="value">unix时间戳，单位：秒</param>
        /// <returns></returns>
        public static DateTime UnixTimeInSeconds2DateTime(long value)
        {
            return (new DateTime(1970, 1, 1) + new TimeSpan((long)(value * Math.Pow(10, 7)))).ToLocalTime();
        }

        /// <summary>
        /// Unix时间戳（单位：毫秒），转本地时间
        /// </summary>
        /// <param name="value">unix时间戳，单位：毫秒</param>
        /// <returns></returns>
        public static DateTime UnixTimeInMillis2DateTime(long value)
        {
            return (new DateTime(1970, 1, 1) + new TimeSpan((long)(value * Math.Pow(10, 4)))).ToLocalTime();
        }

        /// <summary>
        /// 判断是否为手机号
        /// </summary>
        /// <param name="mobile">字符串</param>
        /// <returns></returns>
        public static bool IsMobile(string mobile)
        {
            long temp;
            return mobile.Trim().Length == 11 && long.TryParse(mobile, out temp);
        }
    }
}
